﻿using System;
using System.Collections;
using System.Data.OracleClient;
using TimeTableEasy.src.Database;

namespace TimeTableEasy.src.Core
{
    class Account
    {

        private String _Name, _FirstName, _Adress, _City, _Mail, _PostalCode, _Tel, _DateNaissance;
        private bool _IsAdmin = false, _IsStudent = false, _IsTeacher = false, _IsManager = false;
        private ArrayList _Cursus, _Campus;
        private UInt32 _Id;

        public Account(UInt32 id)
        {
            if (!OracleConnector.Offline)
            {
                OracleDataReader array = OracleConnector.Query("SELECT * FROM IDENTITE WHERE IDE_ID="+id+" AND rownum < 2");

                if (array == null) return;

                array.Read();

                _Id = id;
                _Name = array.GetString(1);
                _FirstName = array.GetString(2);
                _Adress = array.GetString(3);
                _PostalCode = array.GetString(4);
                _City = array.GetString(5);
                _Mail = array.GetString(6);
                _Tel = array.GetString(8);
                _DateNaissance = array.GetString(9);
                _Cursus = new ArrayList();
                _Campus = new ArrayList();

                array = OracleConnector.Query("SELECT 1 FROM ETUDIANT WHERE ETU_NUM="+id+" AND rownum < 2");

                if (array == null) return;

                array.Read();

                _IsStudent = array.HasRows;

                array = OracleConnector.Query("SELECT 1 FROM ENSEIGNANT WHERE ENS_NUM=" + id + " AND rownum < 2");

                if (array == null) return;

                _IsTeacher = array.HasRows;

                array = OracleConnector.Query("SELECT 1 FROM CAMPUSMANAGER WHERE CM_NUM=" + id + " AND rownum < 2");

                if (array == null) return;

                array.Read();

                _IsManager = array.HasRows;

                array = OracleConnector.Query("SELECT 1 FROM ADMINISTRATEUR WHERE ADM_NUM=" + id + " AND rownum < 2");

                if (array == null) return;

                array.Read();

                _IsAdmin = array.HasRows;

                if (_IsStudent)
                {
                    array = OracleConnector.Query("SELECT distinct(EC_CURSUSNOM) FROM ETUDIANT_CURSUS, CURSUS, CURSUS_PERIODE, PERIODE WHERE ETUDIANT_CURSUS.EC_CursusNom = CURSUS.Cur_NomDate and CURSUS.Cur_NomDate = CURSUS_PERIODE.CP_CursusNom and CURSUS_PERIODE.CP_PeriodeNum = PERIODE.Per_Num and EC_ETUDIANTNUM =" + id);

                    if (array == null) return;

                    while (array.Read())
                        _Cursus.Add(array.GetString(0));

                    array = OracleConnector.Query("SELECT CAM_NOM, PER_DATEDEBUT FROM ETUDIANT_CAMPUS_PERIODE, PERIODE, CAMPUS WHERE PERIODE.PER_NUM = ETUDIANT_CAMPUS_PERIODE.ECP_PERIODENUM AND ETUDIANT_CAMPUS_PERIODE.ECP_CAMPUSNUM = CAMPUS.CAM_NUM AND ECP_ETUDIANTNUM = " + id + " ORDER BY (PER_DATEDEBUT)");

                    if (array == null) return;

                    while (array.Read())
                        _Campus.Add(array.GetString(0));
                }

                if (_IsManager)
                {
                    if (!_Campus.Equals(null))
                        _Campus.Clear();

                    array = OracleConnector.Query("SELECT CAM_NOM FROM CAMPUS, CM_CAMPUS WHERE CAMPUS.CAM_NUM = CM_CAMPUS.GER_CAMPUSNUM and GER_CMNUM ="+_Id);

                    if (array == null) return;

                    while (array.Read())
                        _Campus.Add(array.GetString(0));

                }

                if (_IsAdmin)
                {
                    if(!_Campus.Equals(null))
                        _Campus.Clear();

                    array = OracleConnector.Query("SELECT CAM_NOM FROM CAMPUS");

                    if (array == null) return;

                    while (array.Read())
                        _Campus.Add(array.GetString(0));
                }

                if (_Campus.Count > _Cursus.Count)
                    for (int i = 0; i <= _Campus.Count - _Cursus.Count; i++)
                        _Cursus.Add("");

                if (_Cursus.Count > _Campus.Count)
                    for (int i = 0; i <= _Cursus.Count - _Campus.Count; i++)
                        _Campus.Add("");

                array.Dispose();

                Offline.SaveUser();
            }
        }

        public String Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public String FirstName
        {
            get { return _FirstName; }
            set { _FirstName = value; }
        }

        public String Adress
        {
            get { return _Adress; }
            set { _Adress = value; }
        }

        public String City
        {
            get { return _City; }
            set { _City = value; }
        }

        public String Mail
        {
            get { return _Mail; }
            set { _Mail = value; }
        }

        public String PostalCode
        {
            get { return _PostalCode; }
            set { _PostalCode = value; }
        }

        public String DateNaissance
        {
            get { return _DateNaissance; }
            set { _DateNaissance = value; }
        }

        public String Tel
        {
            get { return _Tel; }
            set { _Tel = value; }
        }

        public bool IsAdmin
        {
            get { return _IsAdmin; }
            set { _IsAdmin = value; }
        }

        public bool IsStudent
        {
            get { return _IsStudent; }
            set { _IsStudent = value; }
        }

        public bool IsTeacher
        {
            get { return _IsTeacher; }
            set { _IsTeacher = value; }
        }

        public bool IsManager
        {
            get { return _IsManager; }
            set { _IsManager = value; }
        }

        public ArrayList Cursus
        {
            get { return _Cursus; }
            set { _Cursus = value; }
        }

        public ArrayList Campus
        {
            get { return _Campus; }
            set { _Campus = value; }
        }

        public UInt32 Id
        {
            get { return _Id; }
        }

        public static bool IsAccount(UInt32 id)
        {
            if (!OracleConnector.Offline)
            {
                OracleDataReader array = OracleConnector.Query("SELECT COUNT(*) FROM TIMETABLEASY_APP.IDENTITE WHERE IDE_ID=" + id + " AND rownum < 2");

                if (array == null) return false;

                array.Read();

                if (Int32.Parse(array.GetValue(0).ToString()) == 1)
                    return true;
            }
            else
                return Offline.IsAccount(id); ;      

            return false;
        }

        public static bool CheckPassword(UInt32 id, String password)
        {
            if (!OracleConnector.Offline)
            {
                OracleDataReader array = OracleConnector.Query("SELECT COUNT(*) FROM TIMETABLEASY_APP.IDENTITE WHERE IDE_ID=" + id + " AND IDE_MDP='" + CalculateShaPassHash(password) + "' AND rownum < 2");

                if (array == null) return false;

                array.Read();

                if (Int32.Parse(array.GetValue(0).ToString()) == 1)
                    return true;
            }
            else
                return Offline.CheckPassword(id, password);

            return false;
        }

        public static String CalculateShaPassHash(String password)
        {
            string rethash = "";

            try
            {
                System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
                System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
                byte[] combined = encoder.GetBytes(password);
                hash.ComputeHash(combined);
                rethash = Convert.ToBase64String(hash.Hash);
            }
            catch (Exception ex)
            {
                LogMgr.ERROR_LOG("ACCOUNT EXCEPTION : " + ex.Message);
            }

            return rethash;
        }

        public static ArrayList GetCursus(String campus)
        {
            ArrayList response = new ArrayList();

            OracleDataReader array = OracleConnector.Query("SELECT CC_CursusNum FROM CAMPUS_CURSUS WHERE CC_CampusNum IN (SELECT Cam_Num FROM CAMPUS WHERE Cam_Nom = '" + campus + "')");

            if (array == null) return null;

            while (array.Read())
            {
                response.Add(array.GetString(0));
            }

            array.Dispose();

            return response;
        }

        public static ArrayList GetCampus(UInt32 id)
        {
            ArrayList response = new ArrayList();

            return response;
        }

        ~Account()
        {
            _Cursus.Clear(); 
            _Campus.Clear();
        }
    }
}
